package cn.com.bsoft.cloud.controller.company;

import cn.com.bsoft.cloud.entity.cloud.PrescriptionMess;
import cn.com.bsoft.cloud.entity.cloud.ResultTest;
import cn.com.bsoft.cloud.entity.cloud.Site;
import cn.com.bsoft.cloud.entity.param.DeliveryParam;
import cn.com.bsoft.cloud.entity.param.DrugListParam;
import cn.com.bsoft.cloud.service.CloudCompanyService;
import cn.com.bsoft.cloud.utils.ResultCode;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/cloudcompany/")
public class CloudCompanyController {

    public static Logger logger= LogManager.getLogger(CloudCompanyController.class);

    @Autowired
    private CloudCompanyService cloudCompanyService;


    /**
     * 医药公司调用平台接口获取待配药列表
     * @return
     */
    @ResponseBody
    @RequestMapping("queryDrugList")
    public ResultTest queryDrugList(@RequestBody DrugListParam param){
        ResultTest regMsg=new ResultTest();
        try {
            if (param.getStartDate()==null||(param.getEndDate()==null)) {
                regMsg.setCode(ResultCode.PARAMETERS_EMPTY);
                regMsg.setMsg("开始/结束时间不能为空");
            }
            if (param.getHospitalCodes()==null||param.getHospitalCodes().size()==0) {
                regMsg.setCode(ResultCode.PARAMETERS_EMPTY);
                regMsg.setMsg("医院编码不能为空");
            }
            if (StringUtils.isBlank(param.getPharmaceuticalCompanyCode())) {
                regMsg.setCode(ResultCode.PARAMETERS_EMPTY);
                regMsg.setMsg("医药公司编码不能为空");
            }
            if (StringUtils.isBlank(param.getPharmaceuticalCompanyName())) {
                regMsg.setCode(ResultCode.PARAMETERS_EMPTY);
                regMsg.setMsg("医药公司名称不能为空!");
            }
            List<PrescriptionMess> pmessList= cloudCompanyService.queryDrugList(param);
            if(pmessList==null||pmessList.size()==0){
                regMsg.setCode(ResultCode.SUCCESS);
                regMsg.setMsg("未查到处方列表!");

            }else{
                regMsg.setCode(ResultCode.SUCCESS);
                regMsg.setMsg("查询成功!");
                regMsg.setResult(pmessList);
            }

        } catch (Exception e) {
            regMsg.setCode(ResultCode.ERROR);
            regMsg.setMsg("请求出错!");
            logger.error("医药公司调用平台接口获取待配药列表,参数:"+param+"，异常"+e.getMessage(),e);
        }

        return regMsg;
    }



    /**
     * 医药公司调用平台修改配药状态(并保存操作的医药公司)
     * @return
     */
    @ResponseBody
    @RequestMapping("updateDeliveryStatu")
    public Map<String,Object> updateDeliveryStatu(@RequestBody DeliveryParam param, HttpServletResponse response){
        Map<String,Object> regMsg=new HashMap<String, Object>();

        try {
            if (StringUtils.isBlank(param.getStatus())) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "配送状态不能为空");
                return regMsg;
            }
            if (StringUtils.isBlank(param.getPharmaceuticalCompanyCode())||StringUtils.isBlank(param.getPharmaceuticalCompanyName())) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医药公司信息不能为空");
                return regMsg;
            }
            if (StringUtils.isBlank(param.getHospitalCode())) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医院编码不能为空");
                return regMsg;
            }
            if (StringUtils.isBlank(param.getPrescriptionCode())) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "处方id不能为空");
                return regMsg;
            }
            if (StringUtils.isBlank(param.getCardNo())) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "卡号不能为空");
                return regMsg;
            }
            Map<String,Object> m= cloudCompanyService.updateDeliveryStatu(param);
            regMsg.put("code", m.get("code"));
            regMsg.put("msg", m.get("msg"));

        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("医药公司调用平台修改配药状态,参数:"+param+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }



    /**
     * 根据医院编码查询分站店信息
     * @return
     */
    @ResponseBody
    @RequestMapping("querySitesByCode")
    public Map<String,Object> querySitesByCode(@RequestBody Map<String,String> map){

        Map<String,Object> regMsg=new HashMap<String, Object>();
        try {
            String hoscode=map.get("hospitalCode");
            if (StringUtils.isBlank(hoscode)) {
                regMsg.put("code", ResultCode.PARAMETERS_EMPTY);
                regMsg.put("msg", "医院编码不能为空");
                return regMsg;
            }
            List<Site> list= cloudCompanyService.querySitesByCode(hoscode);
            regMsg.put("code", ResultCode.SUCCESS);
            regMsg.put("msg", "查询成功");
            regMsg.put("result", list);
        } catch (Exception e) {
            regMsg.put("code", ResultCode.ERROR);
            regMsg.put("msg", "请求出错");
            regMsg.put("error", e.getMessage());
            logger.error("查询分站点信息,参数:"+map+"，异常"+e.getMessage(),e);
        }
        return regMsg;
    }

}
